package com.lanxin.bx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanxin.bx.entity.BxInformaldetail;
import com.lanxin.bx.entity.BxInformaltype;
import com.lanxin.bx.service.IBxInformaldetailService;
import com.lanxin.bx.service.IBxInformaltypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cyh
 * @since 2025-07-16
 */
@Controller
@RequestMapping("/bx-informaltype")
public class BxInformaltypeController {

    @Autowired
    private IBxInformaltypeService bxInformaltypeService;
    @Autowired
    private IBxInformaldetailService bxInformaldetailService;

    @RequestMapping("/toinformaltypelist")
    public ModelAndView toinformaltypelist(@RequestParam(required = false) String name,
                                           @RequestParam(defaultValue = "1") Integer page,
                                           @RequestParam(defaultValue = "5") Integer pageSize){
        Page<BxInformaltype> informaltypePage = new Page<>(page, pageSize);
        ModelAndView modelAndView = new ModelAndView();

        QueryWrapper<BxInformaltype> wrapper = new QueryWrapper<>();
        if (Objects.nonNull(name) && !name.isEmpty()) {
            wrapper.like("name", name);
        }

        Page<BxInformaltype> bxInformaltypelist = bxInformaltypeService.page(informaltypePage, wrapper);
        modelAndView.addObject("bxInformaltypelist",bxInformaltypelist);
        modelAndView.addObject("total", bxInformaltypelist.getTotal()); // 总记录数
        modelAndView.addObject("pageNum", bxInformaltypelist.getCurrent()); // 当前页码
        modelAndView.addObject("pageSize", bxInformaltypelist.getSize()); // 每页显示条数
        modelAndView.addObject("totalPages", bxInformaltypelist.getPages()); // 总页数
        modelAndView.addObject("name", name); // 保留搜索条件（回显）
        modelAndView.setViewName("bx/informaltype/informaltypelist");
        return modelAndView;
    }


    @RequestMapping("/toinformaltypedelete")
    @ResponseBody
    public boolean toinformaltypedelete(@RequestParam Integer oid){

        /*bxInformaldetailService*/
        QueryWrapper<BxInformaldetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("typeid",oid);
        long count = bxInformaldetailService.count(queryWrapper);
        if(count > 0){
            return false;
        }
        return bxInformaltypeService.removeById(oid);

    }

    @RequestMapping("/toinformaltypeedit")
    public ModelAndView toinformaltypeedit(@RequestParam(required = false)Integer oid){

        ModelAndView modelAndView = new ModelAndView();
        if (oid != null) {
            BxInformaltype bxInformaltype = bxInformaltypeService.getById(oid);
            if (bxInformaltype != null) {
                modelAndView.addObject("bxInformaltype", bxInformaltype); // 将对象传递给页面
            }
        }
        modelAndView.setViewName("bx/informaltype/informaltypeedit");
        return modelAndView;

    }

    @RequestMapping("/checkBxInformaltypeName")
    @ResponseBody
    public Map<String, Boolean> checkBxInformaltypeName(String name, Integer oid) {
        QueryWrapper<BxInformaltype> wrapper = new QueryWrapper<>();
        wrapper.eq("name", name);
        if (oid != null) {
            wrapper.ne("oid", oid);
        }
        long count = bxInformaltypeService.count(wrapper);

        // 构造JSON对象（包含valid字段）
        Map<String, Boolean> result = new HashMap<>();
        result.put("valid", count == 0);
        return result;
    }

    @RequestMapping("/informaltypeedit")
    public String informaltypeedit(BxInformaltype bxInformaltype, Integer oid){

        ModelAndView modelAndView = new ModelAndView();
        if (bxInformaltype.getIsActice() == null || bxInformaltype.getIsActice().trim().isEmpty()) {
            bxInformaltype.setIsActice("Y");
        }
        if (bxInformaltype.getOid() != null) {
            bxInformaltypeService.updateById(bxInformaltype);
        } else {
            bxInformaltypeService.save(bxInformaltype);
        }
        modelAndView.setViewName("bx/informaltype/informaltypeedit");
        return "redirect:/bx-informaltype/toinformaltypelist";

    }


}
